package com.dragon.modules.yuegou.service.impl.marketing;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.marketing.HomeActivityEntity;
import com.dragon.modules.yuegou.enums.code.BusinessCode;
import com.dragon.modules.yuegou.enums.code.MarketingCode;
import com.dragon.modules.yuegou.enums.marketing.HomeActivityActivityType;
import com.dragon.modules.yuegou.enums.marketing.HomeActivityStatus;
import com.dragon.modules.yuegou.mapper.marketing.HomeActivityMapper;
import com.dragon.modules.yuegou.model.marketing.HomeActivity;
import com.dragon.modules.yuegou.query.marketing.HomeActivityQuery;
import com.dragon.modules.yuegou.service.marketing.HomeActivityService;
import com.dragon.modules.yuegou.utils.PageToResultUtils;
import com.dragon.modules.yuegou.web.Check;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 首页弹框活动表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-08-29 08:45:33
 * @version 2.0
 */
@Slf4j
@Service("mktHomeActivityService")
public class HomeActivityServiceImpl
		extends ServiceImpl<HomeActivityMapper, HomeActivityEntity>
		implements HomeActivityService {

	@Override
	public IPage<HomeActivity> search(PageRequest page, HomeActivityQuery query){
		Check.notNull(query.getBusdId(), BusinessCode.BUSDID_NOTNULL.getMessage());

		QueryWrapper<HomeActivityEntity> wrapper = query.toQueryWrapper();
		wrapper.orderByDesc(HomeActivityEntity.COL_CREATED_TIME);
		IPage<HomeActivity> iPage = page(PageToResultUtils.resultToPage(page), wrapper);
		if(CollectionUtils.isNotEmpty(iPage.getRecords())){
			for (HomeActivity homeActivity : iPage.getRecords()){
				if(HomeActivityActivityType.PRODUCT.equals(homeActivity.getActivityType())){
					homeActivity.setUrl(homeActivity.getSpuTitle());
				}
			}
		}
		return iPage;
	}

	@Override
	public void forbid(String id){
		Check.notEmpty(id, "id不能为空");
		HomeActivityEntity homeActivity = getById(id);
		homeActivity.setStatus(HomeActivityStatus.FORBIDDEN);
		homeActivity.setForbidTime(LocalDateTime.now());
		baseMapper.updateById(homeActivity);
	}

	@Override
	public void saveHomeActivity(HomeActivity homeActivity){
		//校验活动信息
		checkData(homeActivity);
		//保存活动信息
		if(homeActivity.getEndTime().isBefore(LocalDateTime.now())){
			throw new DragonException(MarketingCode.ACTIVITY_IS_EXPIRED);
		}else if(homeActivity.getStartTime().isAfter(LocalDateTime.now())){
			homeActivity.setStatus(HomeActivityStatus.WAITING);
		}else{
			homeActivity.setStatus(HomeActivityStatus.DOING);
		}
		if(StringUtils.isEmpty(homeActivity.getId())){
			save(homeActivity);
		}else {
			baseMapper.updateById(homeActivity);
		}

	}

	private void checkData(HomeActivity homeActivity){
		Check.notEmpty(homeActivity.getBusdId(), BusinessCode.BUSDID_NOTNULL.getMessage());
		Check.notEmpty(homeActivity.getActivityName(), "活动名称不能为空");
		Check.length(homeActivity.getActivityName(), 0, 50, "活动名称50个字内");
		Check.notNull(homeActivity.getActivityType(), "活动类型不能为空");
		Check.notEmpty(homeActivity.getImg(), "图片不能为空");
		if(homeActivity.getActivityType().equals(HomeActivityActivityType.PRODUCT)) {
			Check.notEmpty(homeActivity.getSpuId(), "商品不能为空");
		}else{
			Check.notEmpty(homeActivity.getUrl(), "链接不能为空");
		}
		Check.notNull(homeActivity.getStartTime(), "开始时间不能为空");
		Check.notNull(homeActivity.getEndTime(), "结束时间不能为空");
		Check.notNull(homeActivity.getTimeInterval(), "时间间隔不能为空");
		Check.digits(homeActivity.getTimeInterval(),10, 0, "时间间隔格式不正确");
		if(homeActivity.getTimeInterval() < 0 || homeActivity.getTimeInterval() > 168){
			throw new DragonException(MarketingCode.TIME_INTERVAL_RANGE);
		}
		Check.notNull(homeActivity.getActivityChannel(), "活动渠道不能为空");
	}

	@Override
	public void expireActivity(){
		//执行待开始到进行中
		expireReadyActivity();
		//执行进行中到过期
		expireNormalActivity();
	}

	private void expireReadyActivity(){
		HomeActivityQuery activityQuery = new HomeActivityQuery();
		activityQuery.setStatus(HomeActivityStatus.WAITING);
		List<HomeActivityEntity> list = list(activityQuery.toQueryWrapper());
		int proNum = 0;
		if(!list.isEmpty()){
			for (HomeActivityEntity activity : list){
				if(activity.getStartTime().isBefore(LocalDateTime.now()) && activity.getEndTime().isAfter(LocalDateTime.now())){
					activity.setStatus(HomeActivityStatus.DOING);
					baseMapper.updateById(activity);
				}
				if(activity.getEndTime().isBefore(LocalDateTime.now())){
					activity.setStatus(HomeActivityStatus.EXPIRED);
					baseMapper.updateById(activity);
				}
				proNum++;
			}
		}
		log.info("定时处理首页弹窗活动待开始到进行中,处理条数:" + proNum);
	}

	private void expireNormalActivity(){
		HomeActivityQuery activityQuery = new HomeActivityQuery();
		activityQuery.setStatus(HomeActivityStatus.DOING);
		List<HomeActivityEntity> list = list(activityQuery.toQueryWrapper());
		int proNum = 0;
		if(!list.isEmpty()){
			for (HomeActivityEntity activity : list){
				if(activity.getEndTime().isBefore(LocalDateTime.now())){
					activity.setStatus(HomeActivityStatus.EXPIRED);
					baseMapper.updateById(activity);
				}
				proNum++;
			}
		}
		log.info("定时处理首页弹窗活动进行中到过期,处理条数:" + proNum);
	}

}